package srx.open.com.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import de.greenrobot.event.EventBus;
import srx.open.com.R;
import srx.open.com.bean.User;
import srx.open.com.utils.CommonUtils;
import srx.open.com.utils.Log;
import srx.open.com.utils.OpenToast;
import srx.open.com.utils.UserUtils;

/**
 * 无账号快捷登录界面.
 *
 * @author hailongqiu 356752238@qq.com
 */
public class NoAccountShortcutLoginActivity extends ExtendBaseActivity {

    private static final String TAG = "NoAccountShortcutLoginActivity";

    @Bind(R.id.left_tv)
    TextView left_tv; // 标题栏文本.
    @Bind(R.id.right_lay)
    View right_lay; // 标题栏右侧.
    @Bind(R.id.no_user_et)
    EditText no_user_et; // 手机号码.
    @Bind(R.id.ver_code_btn)
    Button ver_code_btn; // 获取验证码按钮.
    @Bind(R.id.ver_code_et)
    EditText ver_code_et; // 短信验证码.
    @Bind(R.id.ok_login_btn)
    Button ok_login_btn; // 验证并登录.

    @Override
    public void initAllDatas() {
    }

    @Override
    public void initAllViews() {
        setContentView(R.layout.activity_no_account);
        ButterKnife.bind(this);
        left_tv.setText("无账号快捷登录");
        right_lay.setVisibility(View.GONE);
        //
        Intent intent = getIntent();
        if (intent != null) {
            String username = intent.getStringExtra("username");
            if (!TextUtils.isEmpty(username)) {
                phoneStr = username;
                no_user_et.setText(username);
                ver_code_et.setEnabled(true);
                ver_code_et.requestFocus();
                ok_login_btn.setEnabled(true);
            }
        }
        //
        no_user_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                noUserEtAfterTextChange(s);
            }
        });
        //
        ver_code_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                verCodeEtAfterTextChange(s);
            }
        });
    }

    @Override
    public void onLoginSuccess() {
        /* 3. 回到用户主界面 */
        Intent intents = new Intent(mContext, HomeActivity.class);
        startActivity(intents);
		/* 4. 发送界面更新 */
        User user = UserUtils.getCurrentUser(mContext);
        EventBus.getDefault().post(user); // 更新我的页面信息.
        finish();
    }

    @Override
    public void onLoginFailure(int code, String msg) {
        OpenToast.showMsg(msg);
    }

    /*
     * 标题栏返回按钮.
     */
    @OnClick(R.id.left_lay)
    void left_lay(View view) {
        finish();
    }

    /**
     * 手机号码.
     */
    void noUserEtAfterTextChange(Editable text) {
        // 判断是否为有效的手机号码!!
        if (CommonUtils.isMobileNO(text.toString())) {
            ver_code_btn.setEnabled(true);
        } else {
            ver_code_btn.setEnabled(false);
        }
    }

    /**
     * 获取验证码.
     */
    @OnClick(R.id.ver_code_btn)
    void ver_code_btn(View view) {
        phoneStr = no_user_et.getText().toString();
        // 获取验证码.
        BmobSMS.requestSMSCode(mContext, phoneStr, "forgetPass", new RequestSMSCodeListener() {
            @Override
            public void done(Integer integer, BmobException ex) {
                if (ex == null) {
                    OpenToast.showMsg("验证码已发送");
                } else {
                    Log.d(TAG, " BmobSMS.requestSMSCode:" + ex.getErrorCode() + " " + ex.getMessage());
                    OpenToast.showMsg("验证码发送失败");
                }
            }
        });
        //
        ver_code_et.setEnabled(true);
        ver_code_et.requestFocus();
    }

    void verCodeEtAfterTextChange(Editable text) {
        // 判断验证码是否不空.
        if (!TextUtils.isEmpty(text.toString())) {
            ok_login_btn.setEnabled(true);
        } else {
            ok_login_btn.setEnabled(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /*
     * 验证并登录.
     */
    @OnClick(R.id.ok_login_btn)
    void ok_login_btn(View view) {
        String verCode = ver_code_et.getText().toString();
		/* 验证短信 */
        BmobUser.signOrLoginByMobilePhone(this, phoneStr, verCode, new LogInListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (user != null) {
                    OpenToast.showMsg("登陆成功");
                    onLoginSuccess();
                } else {
                    Log.e(TAG, "  BmobUser.signOrLoginByMobilePhone:"  + e.getMessage() + " phoneStr:" + phoneStr + " code:" + e.getErrorCode());
                    OpenToast.showMsg("验证失败");
                }
            }
        });
    }

}
